<?php
/*
Plugin Name: WP-DownloadManager
Plugin URI: http://lesterchan.net/portfolio/programming/php/
Description: Adds a simple download manager to your WordPress blog.
Version: 1.40
Author: Lester 'GaMerZ' Chan
Author URI: http://lesterchan.net
*/


/*  
	Copyright 2008  Lester Chan  (email : lesterchan@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


### Create text domain for translations
add_action('init', 'downloadmanager_textdomain');
function downloadmanager_textdomain() {
	load_plugin_textdomain('wp-downloadmanager', false, 'wp-downloadmanager');
}


### Downloads Table Name
global $wpdb;
$wpdb->downloads = $wpdb->prefix.'downloads';


### Function: Downloads Administration Menu
add_action('admin_menu', 'downloads_menu');
function downloads_menu() {
	if (function_exists('add_menu_page')) {
		add_menu_page(__('Downloads', 'wp-downloadmanager'), __('Downloads', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-manager.php', '', plugins_url('wp-downloadmanager/images/drive.png'));
	}
	if (function_exists('add_submenu_page')) {
		add_submenu_page('wp-downloadmanager/download-manager.php', __('Manage Downloads', 'wp-downloadmanager'), __('Manage Downloads', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-manager.php');
		add_submenu_page('wp-downloadmanager/download-manager.php', __('Add File', 'wp-downloadmanager'), __('Add File', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-add.php');
		add_submenu_page('wp-downloadmanager/download-manager.php', __('Download Options', 'wp-downloadmanager'), __('Download Options', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-options.php');
		add_submenu_page('wp-downloadmanager/download-manager.php', __('Download Templates', 'wp-downloadmanager'), __('Download Templates', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-templates.php');
		add_submenu_page('wp-downloadmanager/download-manager.php', __('Uninstall WP-DownloadManager', 'wp-downloadmanager'), __('Uninstall WP-DownloadManager', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-uninstall.php');
	}
}


### Function: Download Manager CSS
add_action('wp_head', 'downloads_css');
function downloads_css() {
	echo "\n".'<!-- Start Of Script Generated By WP-DownloadManager 1.40 -->'."\n";
	if(@file_exists(TEMPLATEPATH.'/download-css.css')) {
		wp_register_style('wp-downloadmanager', get_stylesheet_directory_uri().'/download-css.css', false, '1.40', 'all');
	} else {
		wp_register_style('wp-downloadmanager', plugins_url('wp-downloadmanager/download-css.css'), false, '1.40', 'all');
	}
	wp_print_styles('wp-downloadmanager');
	echo '<!-- End Of Script Generated By WP-DownloadManager 1.40 -->'."\n";
}


### Function: Displays Download Manager Header In WP-Admin
add_action('admin_head-wp-downloadmanager/download-manager.php', 'downloads_header_admin');
add_action('admin_head-wp-downloadmanager/download-add.php', 'downloads_header_admin');
add_action('admin_head-wp-downloadmanager/download-options.php', 'downloads_header_admin');
add_action('admin_head-wp-downloadmanager/download-templates.php', 'downloads_header_admin');
add_action('admin_head-wp-downloadmanager/download-uninstall.php', 'downloads_header_admin');
function downloads_header_admin() {
	wp_register_style('wp-downloadmanager-admin', plugins_url('wp-downloadmanager/download-admin-css.css'), false, '1.40', 'all');
	echo "\n".'<!-- Start Of Script Generated By WP-DownloadManager 1.40 -->'."\n";
	wp_print_styles('wp-downloadmanager-admin');
	echo '<!-- End Of Script Generated By WP-DownloadManager 1.40 -->'."\n";
}


### Function: Displays Download Manager Footer In WP-Admin
add_action('admin_footer', 'downloads_footer_admin');
function downloads_footer_admin() {
	// Javascript Code Courtesy Of WP-AddQuicktag (http://bueltge.de/wp-addquicktags-de-plugin/120/)
	echo '<script type="text/javascript">'."\n";
	echo "\t".'function insertDownload(where, myField) {'."\n";
	echo "\t\t".'var download_id = prompt("'.js_escape(__('Enter File ID (Separate Multiple IDs By A Comma)', 'wp-downloadmanager')).'");'."\n";
	echo "\t\t".'if(where == "code") {'."\n";
	echo "\t\t\t".'edInsertContent(myField, "[download id=\"" + download_id + "\"]");'."\n";
	echo "\t\t".'} else {'."\n";
	echo "\t\t\t".'return "[download id=\"" + download_id + "\"]";'."\n";
	echo "\t\t".'}'."\n";
	echo "\t".'}'."\n";
	echo "\t".'if(document.getElementById("ed_toolbar")){'."\n";
	echo "\t\t".'qt_toolbar = document.getElementById("ed_toolbar");'."\n";
	echo "\t\t".'edButtons[edButtons.length] = new edButton("ed_downloadmanager","'.js_escape(__('Download', 'wp-downloadmanager')).'", "", "","");'."\n";
	echo "\t\t".'var qt_button = qt_toolbar.lastChild;'."\n";
	echo "\t\t".'while (qt_button.nodeType != 1){'."\n";
	echo "\t\t\t".'qt_button = qt_button.previousSibling;'."\n";
	echo "\t\t".'}'."\n";
	echo "\t\t".'qt_button = qt_button.cloneNode(true);'."\n";
	echo "\t\t".'qt_button.value = "'.js_escape(__('Download', 'wp-downloadmanager')).'";'."\n";
	echo "\t\t".'qt_button.title = "'.js_escape(__('Insert File Download', 'wp-downloadmanager')).'";'."\n";
	echo "\t\t".'qt_button.onclick = function () { insertDownload(\'code\', edCanvas);}'."\n";
	echo "\t\t".'qt_button.id = "ed_downloadmanager";'."\n";
	echo "\t\t".'qt_toolbar.appendChild(qt_button);'."\n";
	echo "\t".'}'."\n";
	echo '</script>'."\n";
}


### Function: Add Quick Tag For Poll In TinyMCE >= WordPress 2.5
add_action('init', 'download_tinymce_addbuttons');
function download_tinymce_addbuttons() {
	if(!current_user_can('edit_posts') && ! current_user_can('edit_pages')) {
		return;
	}
	if(get_user_option('rich_editing') == 'true') {
		add_filter("mce_external_plugins", "download_tinymce_addplugin");
		add_filter('mce_buttons', 'download_tinymce_registerbutton');
	}
}
function download_tinymce_registerbutton($buttons) {
	array_push($buttons, 'separator', 'downloadmanager');
	return $buttons;
}
function download_tinymce_addplugin($plugin_array) {
	$plugin_array['downloadmanager'] = plugins_url('wp-downloadmanager/tinymce/plugins/downloadmanager/editor_plugin.js');
	return $plugin_array;
}


### Function: Add Download Query Vars
add_filter('query_vars', 'download_query_vars');
function download_query_vars($public_query_vars) {
	$public_query_vars[] = "dl_id";
	return $public_query_vars;
}


### Function: Download htaccess ReWrite Rules   
add_filter('generate_rewrite_rules', 'download_rewrite');
function download_rewrite($wp_rewrite) {
	$wp_rewrite->rules = array_merge(array('download/([0-9]{1,})/?$' => 'index.php?dl_id=$matches[1]'), $wp_rewrite->rules);
}


### Function: Download File
add_action('template_redirect', 'download_file', 5);
function download_file() {
	global $wpdb, $user_ID;
	$id = intval(get_query_var('dl_id'));
	if($id > 0) {
		$file_path = stripslashes(get_option('download_path'));
		$file_url = stripslashes(get_option('download_path_url'));
		$download_method = intval(get_option('download_method'));
		$file = $wpdb->get_row("SELECT file, file_permission FROM $wpdb->downloads WHERE file_id = $id AND file_permission != -1");
		if(!$file) {
			header('HTTP/1.0 404 Not Found');
			die(__('Invalid File ID.', 'wp-downloadmanager'));
		}
		if(($file->file_permission == 1 && intval($user_ID) > 0) || $file->file_permission == 0) {
			$update_hits = $wpdb->query("UPDATE $wpdb->downloads SET file_hits = file_hits + 1 WHERE file_id = $id AND file_permission != -1");
			$update_last_downloaded_date = $wpdb->query("UPDATE $wpdb->downloads SET file_last_downloaded_date = '".current_time('timestamp')."' WHERE file_id = $id AND file_permission != -1");
			$file_name = stripslashes($file->file);
			if(!is_remote_file($file_name)) {
				if(!is_file($file_path.$file_name)) {
					header('HTTP/1.0 404 Not Found');
					die(__('File does not exist.', 'wp-downloadmanager'));
				}
				if($download_method == 0) {
					header("Pragma: public");
					header("Expires: 0");
					header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
					header("Content-Type: application/force-download");
					header("Content-Type: application/octet-stream");
					header("Content-Type: application/download");
					header("Content-Disposition: attachment; filename=".basename($file_name).";");
					header("Content-Transfer-Encoding: binary");
					header("Content-Length: ".filesize($file_path.$file_name));
					@readfile($file_path.$file_name);
				} else {
					header('Location: '.$file_url.$file_name);
				}
				exit();
			} else {
				if(ini_get('allow_url_fopen') && $download_method == 0) {
					header("Pragma: public");
					header("Expires: 0");
					header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
					header("Content-Type: application/force-download");
					header("Content-Type: application/octet-stream");
					header("Content-Type: application/download");
					header("Content-Disposition: attachment; filename=".basename($file_name).";");
					header("Content-Transfer-Encoding: binary");
					$file_size = remote_filesize($file_name);					
					if($file_size != __('unknown', 'wp-downloadmanager')) {
						header("Content-Length: ".$file_size);
					}
					@readfile($file_name);
				} else {
					header('Location: '.$file_name);					
				}
				exit();
			}
		} else {
			_e('You need to be a registered user to download this file.', 'wp-downloadmanager');
			exit();
		}
	}
}


### Function: Print Out File Extension Image
function file_extension_image($file_name, $file_ext_images) {
	$file_ext = file_extension($file_name);
	$file_ext .= '.gif';
	if(in_array($file_ext, $file_ext_images)) {
		return $file_ext;
	} else {
		return 'unknown.gif';
	}
}


### Function: Get File Extension Images
function file_extension_images() {
	$file_ext_images = array();
	$dir = WP_PLUGIN_DIR.'/wp-downloadmanager/images';
	if (is_dir($dir)) {
	   if ($dh = opendir($dir)) {
		   while (($file = readdir($dh)) !== false) {
				if($file != '.' && $file != '..')	{
					$file_ext_images[] = $file;
				}
		   }
		   closedir($dh);
	   }
	}
	return $file_ext_images;
}


### Function: Get File Extension
if(!function_exists('file_extension')) {
	function file_extension($filename) {
		$file_ext = explode('.', $filename);
		$file_ext = $file_ext[sizeof($file_ext)-1];
		$file_ext = strtolower($file_ext);
		return $file_ext;
	}
}


### Function: Get Remote File Size
if(!function_exists('remote_filesize')) {
	function remote_filesize($uri) {
		$header_array = @get_headers($uri, 1);
		$file_size = $header_array['Content-Length'];
		if(!empty($file_size)) {
			return $file_size;
		} else {
			return __('unknown', 'wp-downloadmanager');
		}
	}
}


### Function: Format Bytes Into TiB/GiB/MiB/KiB/Bytes
if(!function_exists('format_filesize')) {
	function format_filesize($rawSize) {
		if($rawSize / 1099511627776 > 1) {
			return number_format_i18n($rawSize/1099511627776, 1).' '.__('TiB', 'wp-downloadmanager');
		} elseif($rawSize / 1073741824 > 1) {
			return number_format_i18n($rawSize/1073741824, 1).' '.__('GiB', 'wp-downloadmanager');
		} elseif($rawSize / 1048576 > 1) {
			return number_format_i18n($rawSize/1048576, 1).' '.__('MiB', 'wp-downloadmanager');
		} elseif($rawSize / 1024 > 1) {
			return number_format_i18n($rawSize/1024, 1).' '.__('KiB', 'wp-downloadmanager');
		} elseif($rawSize > 1) {
			return number_format_i18n($rawSize, 0).' '.__('bytes', 'wp-downloadmanager');
		} else {
			return __('unknown', 'wp-downloadmanager');
		}
	}
}


### Function: Get Max File Size That Can Be Uploaded
function get_max_upload_size() {
	$maxsize = ini_get('upload_max_filesize');
	if (!is_numeric($maxsize)) {
		if (strpos($maxsize, 'M') !== false) {
			$maxsize = intval($maxsize)*1024*1024;
		} elseif (strpos($maxsize, 'K') !== false) {
			$maxsize = intval($maxsize)*1024;
		} elseif (strpos($maxsize, 'G') !== false) {
			$maxsize = intval($maxsize)*1024*1024*1024;
		}
	}
	return $maxsize;
}


### Function: Is Remote File
function is_remote_file($file_name) {
	if(strpos($file_name, 'http://') === false && strpos($file_name, 'https://') === false  && strpos($file_name, 'ftp://') === false) {
		return false;
	}
	return true;
}


### Function: Snippet Text
if(!function_exists('snippet_text')) {
	function snippet_text($text, $length = 0) {
		if (defined('MB_OVERLOAD_STRING')) {
		  $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
		 	if (mb_strlen($text) > $length) {
				return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
		 	} else {
				return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
		 	}
		} else {
			$text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
		 	if (strlen($text) > $length) {
				return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
		 	} else {
				return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
		 	}
		}
	}
}


### Function: Download URL
function download_file_url($file_id) {
	$file_id = intval($file_id);
	if(intval(get_option('download_nice_permalink')) == 1) {
		$download_file_url = get_option('home').'/download/'.$file_id.'/';
	} else {
		$download_file_url =  get_option('home').'/?dl_id='.$file_id;
	}
	return $download_file_url;
}


### Function: Download Category URL
function download_category_url($cat_id) {
	$download_page_url = get_option('download_page_url');
	if(strpos($download_page_url, '?') !== false) {
		$download_page_url = "$download_page_url&amp;dl_cat=$cat_id";
	} else {
		$download_page_url = "$download_page_url?dl_cat=$cat_id";
	}
	return $download_page_url;
}


### Function: Download Page Link
function download_page_link($page) {
	$current_url = $_SERVER['REQUEST_URI'];
	$curren_downloadpage = intval($_GET['dl_page']);
	$download_page_link = preg_replace('/dl_page=(\d+)/i', 'dl_page='.$page, $current_url);
	if($curren_downloadpage == 0) {
		if(strpos($current_url, '?') !== false) {
			$download_page_link = "$download_page_link&amp;dl_page=$page";
		} else {
			$download_page_link = "$download_page_link?dl_page=$page";
		}
	}
	return $download_page_link;
}


### Function Highlight Download Search
function download_search_highlight($search_word, $search_text) {
	if(!empty($search_word)) {
		$search_words_array = explode(' ', $search_word);
		foreach($search_words_array as $search_word_array) {
			$search_text = eregi_replace('('.quotemeta($search_word_array).')', '<span class="download-search-highlight">\\1</span>', $search_text);
		}
	}
	return $search_text;
}


### Function: Short Code For Inserting Downloads Page Into Page
add_shortcode('page_download', 'download_page_shortcode');
add_shortcode('page_downloads', 'download_page_shortcode');
function download_page_shortcode($atts) {
	extract(shortcode_atts(array('category' => 0), $atts));
	return downloads_page($category);
}


### Function: Short Code For Inserting Files Download Into Posts
add_shortcode('download', 'download_shortcode');
function download_shortcode($atts) {
	extract(shortcode_atts(array('id' => '0', 'category' => '0', 'display' => 'both'), $atts));
	if(!is_feed()) {
		$conditions = array();
		if($id != '0') {
			if(strpos($id, ',') !== false) {
				$conditions[] = "file_id IN ($id)";
			} else {
				$conditions[] = "file_id = $id";
			}
		}
		if($category != '0') {
			if(strpos($category, ',') !== false) {
				$conditions[] = "file_category IN ($category)";
			} else {
				$conditions[] = "file_category = $category";
			}
		}
		if($conditions) {
			return download_embedded(implode(' AND ', $conditions), $display);
		}
		else {
			return '';
		}
	} else {
		return __('Note: There is a file embedded within this post, please visit this post to download the file.', 'wp-downloadmanager');
	}
}


### Function: Downloads Page
function downloads_page($category_id = 0) {
	global $wpdb, $user_ID;
	// Variables
	$category_id = intval($category_id);
	$category = intval($_GET['dl_cat']);
	$page = intval($_GET['dl_page']);
	$search_word = strip_tags(addslashes(trim($_GET['dl_search'])));
	$search_words_array = array();
	$search = stripslashes($search_word);	
	$download_categories = get_option('download_categories');
	$download_categories[0] = __('total', 'wp-downloadmanager');
	$category_stats = array();
	$total_stats = array('files' => 0, 'size' => 0, 'hits' => 0);
	$file_sort = get_option('download_sort');
	$file_extensions_images = file_extension_images();
	// If There Is Category Set
	$category_sql = '';
	if($category ==  0 && $category_id > 0) {
		$category = $category_id;
	}
	if($category > 0) {
		$category_sql = "AND file_category = $category";
	}
	// If There Is A Search Term
	$search_sql = '';
	if(!empty($search)) {
		$search_words_array = explode(' ', $search_word);
		foreach($search_words_array as $search_word_array) {
			$search_sql .= " AND ((file_name LIKE('%$search_word_array%') OR file_des LIKE ('%$search_word_array%')))";
		}
	}
	// Calculate Categories And Total Stats
	$categories = $wpdb->get_results("SELECT file_category, COUNT(file_id) as category_files, SUM(file_size) category_size, SUM(file_hits) as category_hits FROM $wpdb->downloads WHERE 1=1 $category_sql $search_sql AND file_permission != -1 GROUP BY file_category");
	if($categories) {
		foreach($categories as $cat) {
			$cat_id = intval($cat->file_category);
			$category_stats[$cat_id]['files'] = $cat->category_files;
			$category_stats[$cat_id]['hits'] = $cat->category_hits;
			$category_stats[$cat_id]['size'] = $cat->category_size;
			$total_stats['files'] +=$cat->category_files;
			$total_stats['hits'] += $cat->category_hits;
			$total_stats['size'] += $cat->category_size;
		}
	}

	// Calculate Paging
	$numposts = $total_stats['files'];
	$perpage = $file_sort['perpage'];
	$max_page = ceil($numposts/$perpage);	
	if(empty($page) || $page == 0) {
		$page = 1;
	}
	$offset = ($page-1) * $perpage;
	$pages_to_show = 10;
	$pages_to_show_minus_1 = $pages_to_show-1;
	$half_page_start = floor($pages_to_show_minus_1/2);
	$half_page_end = ceil($pages_to_show_minus_1/2);
	$start_page = $page - $half_page_start;
	if($start_page <= 0) {
		$start_page = 1;
	}
	$end_page = $page + $half_page_end;
	if(($end_page - $start_page) != $pages_to_show_minus_1) {
		$end_page = $start_page + $pages_to_show_minus_1;
	}
	if($end_page > $max_page) {
		$start_page = $max_page - $pages_to_show_minus_1;
		$end_page = $max_page;
	}
	if($start_page <= 0) {
		$start_page = 1;
	}
	if(($offset + $perpage) > $numposts) { 
		$max_on_page = $numposts; 
	} else { 
		$max_on_page = ($offset + $perpage); 
	}
	if (($offset + 1) > ($numposts)) { 
		$display_on_page = $numposts; 
	} else { 
		$display_on_page = ($offset + 1); 
	}

	// Get Sorting Group
	$group_sql = '';
	if($file_sort['group'] == 1) {
		$group_sql = 'file_category ASC,';
	}
	// Get Files
	$files = $wpdb->get_results("SELECT * FROM $wpdb->downloads WHERE 1=1 $category_sql $search_sql AND file_permission != -1 ORDER BY $group_sql {$file_sort['by']} {$file_sort['order']} LIMIT $offset, {$file_sort['perpage']}");
	if($files) {
		// Get Download Page Header
		$template_download_header = stripslashes(get_option('download_template_header'));
		if(get_option('download_nice_permalink') == '0' && preg_match('/[\?\&]page_id=(\d+)/i', get_option('download_page_url'), $matches)) {
			$template_download_header = preg_replace('/(<form[^>]+>)/i', '$1<input type="hidden" name="page_id" value="'.$matches[1].'" />', $template_download_header);
		}
		$template_download_header = str_replace("%TOTAL_FILES_COUNT%", number_format_i18n($total_stats['files']), $template_download_header);
		$template_download_header = str_replace("%TOTAL_HITS%", number_format_i18n($total_stats['hits']), $template_download_header);
		$template_download_header = str_replace("%TOTAL_SIZE%", format_filesize($total_stats['size']), $template_download_header);
		$template_download_header = str_replace("%RECORD_START%", number_format_i18n($display_on_page), $template_download_header);
		$template_download_header = str_replace("%RECORD_END%", number_format_i18n($max_on_page), $template_download_header);
		$template_download_header = str_replace("%CATEGORY_ID%", $category, $template_download_header);
		$template_download_header = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$category]), $template_download_header);
		$template_download_header = str_replace("%FILE_SEARCH_WORD%", $search, $template_download_header);
		$template_download_header = str_replace("%DOWNLOAD_PAGE_URL%", get_option('download_page_url'), $template_download_header);
		$output = $template_download_header;
		// Loop Through Files
		$i = 1;
		$k = 1;
		$temp_cat_id = -1;
		$need_footer = 0;
		foreach($files as $file) {
			$cat_id = intval($file->file_category);
			// Print Out Category Footer
			if($need_footer && $temp_cat_id != $cat_id && $file_sort['group'] == 1) {
				// Get Download Category Footer
				$template_download_category_footer = stripslashes(get_option('download_template_category_footer'));
				$template_download_category_footer = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$cat_id]), $template_download_category_footer);
				$template_download_category_footer = str_replace("%CATEGORY_ID%", $cat_id, $template_download_category_footer);
				$template_download_category_footer = str_replace("%CATEGORY_URL%", download_category_url($cat_id), $template_download_category_footer);
				$template_download_category_footer = str_replace("%CATEGORY_FILES_COUNT%", number_format_i18n($category_stats[$cat_id]['files']), $template_download_category_footer);
				$template_download_category_footer = str_replace("%CATEGORY_HITS%", number_format_i18n($category_stats[$cat_id]['hits']), $template_download_category_footer);
				$template_download_category_footer = str_replace("%CATEGORY_SIZE%", format_filesize($category_stats[$cat_id]['size']), $template_download_category_footer);
				$output .= $template_download_category_footer;
				$need_footer = 0;
			}
			// Print Out Category Header
			if($temp_cat_id != $cat_id && $file_sort['group'] == 1) {
				// Get Download Category Header
				$template_download_category_header = stripslashes(get_option('download_template_category_header'));
				$template_download_category_header = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$cat_id]), $template_download_category_header);
				$template_download_category_header = str_replace("%CATEGORY_ID%", $cat_id, $template_download_category_header);
				$template_download_category_header = str_replace("%CATEGORY_URL%", download_category_url($cat_id), $template_download_category_header);
				$template_download_category_header = str_replace("%CATEGORY_FILES_COUNT%", number_format_i18n($category_stats[$cat_id]['files']), $template_download_category_header);
				$template_download_category_header = str_replace("%CATEGORY_HITS%", number_format_i18n($category_stats[$cat_id]['hits']), $template_download_category_header);
				$template_download_category_header = str_replace("%CATEGORY_SIZE%", format_filesize($category_stats[$cat_id]['size']), $template_download_category_header);
				$output .= $template_download_category_header;
				$i = 1;
				$need_footer = 1;
			}
			// Get Download Listing
			$template_download_listing = get_option('download_template_listing');
			if(($file->file_permission == 1 && intval($user_ID) > 0) || $file->file_permission == 0) {
				$template_download_listing = stripslashes($template_download_listing[0]);
			} else {
				$template_download_listing = stripslashes($template_download_listing[1]);
			}
			$template_download_listing = str_replace("%FILE_ID%", $file->file_id, $template_download_listing);
			$template_download_listing = str_replace("%FILE%", stripslashes($file->file), $template_download_listing);
			$template_download_listing = str_replace("%FILE_NAME%", download_search_highlight($search, stripslashes($file->file_name)), $template_download_listing);
			$template_download_listing = str_replace("%FILE_ICON%", file_extension_image(stripslashes($file->file), $file_extensions_images), $template_download_listing);
			$template_download_listing = str_replace("%FILE_DESCRIPTION%", download_search_highlight($search, stripslashes($file->file_des)), $template_download_listing);
			$template_download_listing = str_replace("%FILE_SIZE%",  format_filesize($file->file_size), $template_download_listing);
			$template_download_listing = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$cat_id]), $template_download_listing);
			$template_download_listing = str_replace("%FILE_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_listing);
			$template_download_listing = str_replace("%FILE_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_listing);
			$template_download_listing = str_replace("%FILE_UPDATED_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_listing);
			$template_download_listing = str_replace("%FILE_UPDATED_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_listing);
			$template_download_listing = str_replace("%FILE_HITS%", number_format_i18n($file->file_hits), $template_download_listing);
			$template_download_listing = str_replace("%FILE_DOWNLOAD_URL%", download_file_url($file->file_id), $template_download_listing);
			$output .= $template_download_listing;
			// Assign Cat ID To Temp Cat ID
			$temp_cat_id = $cat_id;
			// Count Files
			$i++;
			$k++;
		}
		// Print Out Category Footer
		if($need_footer) {
			// Get Download Category Footer
			$template_download_category_footer = stripslashes(get_option('download_template_category_footer'));
			$template_download_category_footer = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$cat_id]), $template_download_category_footer);
			$template_download_category_footer = str_replace("%CATEGORY_ID%", $cat_id, $template_download_category_footer);
			$template_download_category_footer = str_replace("%CATEGORY_URL%", download_category_url($cat_id), $template_download_category_footer);
			$template_download_category_footer = str_replace("%CATEGORY_FILES_COUNT%", number_format_i18n($category_stats[$cat_id]['files']), $template_download_category_footer);
			$template_download_category_footer = str_replace("%CATEGORY_HITS%", number_format_i18n($category_stats[$cat_id]['hits']), $template_download_category_footer);
			$template_download_category_footer = str_replace("%CATEGORY_SIZE%", format_filesize($category_stats[$cat_id]['size']), $template_download_category_footer);
			$output .= $template_download_category_footer;
			$need_footer = 0;
		}
		// Get Download Page Footer
		$template_download_footer = stripslashes(get_option('download_template_footer'));
		if(get_option('download_nice_permalink') == '0' && preg_match('/[\?\&]page_id=(\d+)/i', get_option('download_page_url'), $matches)) {
			$template_download_footer = preg_replace('/(<form[^>]+>)/i', '$1<input type="hidden" name="page_id" value="'.$matches[1].'" />', $template_download_footer);
		}		
		$template_download_footer = str_replace("%TOTAL_FILES_COUNT%", number_format_i18n($total_stats['files']), $template_download_footer);
		$template_download_footer = str_replace("%TOTAL_HITS%", number_format_i18n($total_stats['hits']), $template_download_footer);
		$template_download_footer = str_replace("%TOTAL_SIZE%", format_filesize($total_stats['size']), $template_download_footer);
		$template_download_footer = str_replace("%CATEGORY_ID%", $category, $template_download_footer);
		$template_download_footer = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$category]), $template_download_footer);
		$template_download_footer = str_replace("%FILE_SEARCH_WORD%", $search, $template_download_footer);
		$template_download_footer = str_replace("%DOWNLOAD_PAGE_URL%", get_option('download_page_url'), $template_download_footer);
		$output .= $template_download_footer;
	} else {
		$template_download_none = stripslashes(get_option('download_template_none'));
		$output .= $template_download_none;
	}
	// Download Paging
	if($max_page > 1) {
		$output .= stripslashes(get_option('download_template_pagingheader'));
		if(function_exists('wp_pagenavi')) {
			$output .= '<div class="wp-pagenavi">'."\n";
		} else {
			$output .= '<div class="wp-downloadmanager-paging">'."\n";
		}
		$output .= '<span class="pages">&#8201;'.sprintf(__('Page %s of %s', 'wp-downloadmanager'), number_format_i18n($page), number_format_i18n($max_page)).'&#8201;</span>';			
		if ($start_page >= 2 && $pages_to_show < $max_page) {
			$output .= '<a href="'.download_page_link(1).'" title="'.__('&laquo; First', 'wp-downloadmanager').'">&#8201;'.__('&laquo; First', 'wp-downloadmanager').'&#8201;</a>';
			$output .= '<span class="extend">...</span>';
		}
		if($page > 1) {
			$output .= '<a href="'.download_page_link(($page-1)).'" title="'.__('&laquo;', 'wp-downloadmanager').'">&#8201;'.__('&laquo;', 'wp-downloadmanager').'&#8201;</a>';
		}
		for($i = $start_page; $i  <= $end_page; $i++) {						
			if($i == $page) {
				$output .= '<span class="current">&#8201;'.number_format_i18n($i).'&#8201;</span>';
			} else {
				$output .= '<a href="'.download_page_link($i).'" title="'.number_format_i18n($i).'">&#8201;'.number_format_i18n($i).'&#8201;</a>';
			}
		}
		if(empty($page) || ($page+1) <= $max_page) {
			$output .= '<a href="'.download_page_link(($page+1)).'" title="'.__('&raquo;', 'wp-downloadmanager').'">&#8201;'.__('&raquo;', 'wp-downloadmanager').'&#8201;</a>';
		}
		if ($end_page < $max_page) {
			$output .= '<span class="extend">...</span>';
			$output .= '<a href="'.download_page_link($max_page).'" title="'.__('Last &raquo;', 'wp-downloadmanager').'">&#8201;'.__('Last &raquo;', 'wp-downloadmanager').'&#8201;</a>';
		}
		$output .= '</div>';
		$output .= stripslashes(get_option('download_template_pagingfooter'));
	}
	return apply_filters('downloads_page', $output);
}


### Function: List Out All Files In Downloads Directory
function list_downloads_files($dir, $orginal_dir) {
	global $download_files, $download_files_subfolder;
	if (is_dir($dir)) {
	   if ($dh = opendir($dir)) {
		   while (($file = readdir($dh)) !== false) {
				if($file != '.' && $file != '..' && $file != '.htaccess')	{
					if(is_dir($dir.'/'.$file)) {						
						list_downloads_files($dir.'/'.$file, $orginal_dir);
					} else {
						$folder_file =str_replace($orginal_dir, '', $dir.'/'.$file);
						$sub_dir = explode('/', $folder_file);
						if(sizeof($sub_dir)  > 2) {
							$download_files_subfolder[] = $folder_file;
						} else {
							$download_files[] = $folder_file;
						}
					}
				}
		   }
		   closedir($dh);
	   }
	}
}


### Function: List Out All Files In Downloads Directory
function list_downloads_folders($dir, $orginal_dir) {
	global $download_folders;
	if (is_dir($dir)) {
	   if ($dh = opendir($dir)) {
		   while (($file = readdir($dh)) !== false) {
				if($file != '.' && $file != '..')	{
					if(is_dir($dir.'/'.$file)) {
						$folder =str_replace($orginal_dir, '', $dir.'/'.$file);
						$download_folders[] = $folder;
						list_downloads_folders($dir.'/'.$file, $orginal_dir);
					}
				}
		   }
		   closedir($dh);
	   }
	}
}


### Function: Print Listing Of Files In Alphabetical Order
function print_list_files($dir, $orginal_dir, $selected = '') {
	global $download_files, $download_files_subfolder;
	list_downloads_files($dir, $orginal_dir);
	if($download_files) {
		natcasesort($download_files);
	}
	if($download_files_subfolder) {
		natcasesort($download_files_subfolder);
	}
	if($download_files) {
		foreach($download_files as $download_file) {
			if($download_file == $selected) {
				echo '<option value="'.$download_file.'" selected="selected">'.$download_file.'</option>'."\n";	
			} else {
				echo '<option value="'.$download_file.'">'.$download_file.'</option>'."\n";	
			}
		}
	}
	if($download_files_subfolder) {
		foreach($download_files_subfolder as $download_file_subfolder) {
			if($download_file_subfolder == $selected) {
				echo '<option value="'.$download_file_subfolder.'" selected="selected">'.$download_file_subfolder.'</option>'."\n";	
			} else {
				echo '<option value="'.$download_file_subfolder.'">'.$download_file_subfolder.'</option>'."\n";	
			}
		}
	}
}


### Function: Print Listing Of Folders In Alphabetical Order
function print_list_folders($dir, $orginal_dir) {
	global $download_folders;
	list_downloads_folders($dir, $orginal_dir);
	if($download_folders) {
		natcasesort($download_folders);
		echo '<option value="/">/</option>'."\n";	
		foreach($download_folders as $download_folder) {
			echo '<option value="'.$download_folder.'">'.$download_folder.'</option>'."\n";	
		}
	}
}


### Function: Rename File To Ensure (Credits: imvain2)
function download_rename_file($file_path, $file) {
	$rename = false;
	$file_old = $file;
	$file = str_replace(' ', '_', $file);
	$file = preg_replace('/[^A-Za-z0-9\-._\/]/', '', $file);
	if($file != $file_old) {
		$rename = rename($file_path.$file_old, $file_path.$file);
	}
	if($rename) {
		return $file;
	} else {
		return $file_old;
	}
}


### Function: Editable Timestamp
function file_timestamp($file_timestamp) {
	global $month;
	$day = gmdate('j', $file_timestamp);
	echo '<select id="file_timestamp_day" name="file_timestamp_day" size="1">'."\n";
	for($i = 1; $i <=31; $i++) {
		if($day == $i) {
			echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";	
		} else {
			echo "<option value=\"$i\">$i</option>\n";	
		}
	}
	echo '</select>&nbsp;&nbsp;'."\n";
	$month2 = gmdate('n', $file_timestamp);
	echo '<select id="file_timestamp_month" name="file_timestamp_month" size="1">'."\n";
	for($i = 1; $i <= 12; $i++) {
		if ($i < 10) {
			$ii = '0'.$i;
		} else {
			$ii = $i;
		}
		if($month2 == $i) {
			echo "<option value=\"$i\" selected=\"selected\">$month[$ii]</option>\n";	
		} else {
			echo "<option value=\"$i\">$month[$ii]</option>\n";	
		}
	}
	echo '</select>&nbsp;&nbsp;'."\n";
	$year = gmdate('Y', $file_timestamp);
	echo '<select id="file_timestamp_year" name="file_timestamp_year" size="1">'."\n";
	for($i = 2000; $i <= gmdate('Y'); $i++) {
		if($year == $i) {
			echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";	
		} else {
			echo "<option value=\"$i\">$i</option>\n";	
		}
	}
	echo '</select>&nbsp;@'."\n";
  echo '<span dir="ltr">'."\n";
	$hour = gmdate('H', $file_timestamp);
	echo '<select id="file_timestamp_hour" name="file_timestamp_hour" size="1">'."\n";
	for($i = 0; $i < 24; $i++) {
		if($hour == $i) {
			echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";	
		} else {
			echo "<option value=\"$i\">$i</option>\n";	
		}
	}
	echo '</select>&nbsp;:'."\n";
	$minute = gmdate('i', $file_timestamp);
	echo '<select id="file_timestamp_minute" name="file_timestamp_minute" size="1">'."\n";
	for($i = 0; $i < 60; $i++) {
		if($minute == $i) {
			echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";	
		} else {
			echo "<option value=\"$i\">$i</option>\n";	
		}
	}
	
	echo '</select>&nbsp;:'."\n";
	$second = gmdate('s', $file_timestamp);
	echo '<select id="file_timestamp_second" name="file_timestamp_second" size="1">'."\n";
	for($i = 0; $i <= 60; $i++) {
		if($second == $i) {
			echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";	
		} else {
			echo "<option value=\"$i\">$i</option>\n";	
		}
	}
	echo '</select>'."\n";
  echo '</span>'."\n";
}


### Function: Get Total Download Files
function get_download_files($display = true) {
	global $wpdb;
	$totalfiles = $wpdb->get_var("SELECT COUNT(file_id) FROM $wpdb->downloads");
	if($display) {
		echo number_format_i18n($totalfiles);
	} else {
		return number_format_i18n($totalfiles);
	}
}


### Function Get Total Download Size
function get_download_size($display = true) {
	global $wpdb;
	$totalsize = $wpdb->get_var("SELECT SUM(file_size) FROM $wpdb->downloads");
	if($display) {
		echo format_filesize($totalsize);
	} else {
		return format_filesize($totalsize);
	}
}


### Function: Get Total Download Hits
function get_download_hits($display = true) {
	global $wpdb;
	$totalhits = $wpdb->get_var("SELECT SUM(file_hits) FROM $wpdb->downloads");
	if($display) {
		echo number_format_i18n($totalhits);
	} else {
		return number_format_i18n($totalhits);
	}
}


### Function: Download Embedded
function download_embedded($condition = '', $display = 'both') {
	global $wpdb, $user_ID;
	$output = '';
	$file_extensions_images = file_extension_images();
	if($condition !== '') {
		$condition .= ' AND ';
	}
	$files = $wpdb->get_results("SELECT * FROM $wpdb->downloads WHERE $condition file_permission != -1");
	if($files) {
		// Get Download Categories
		$download_categories = get_option('download_categories');
		// Get Embedded
		$template_download_embedded_temp = get_option('download_template_embedded');
		foreach($files as $file) {
			$template_download_embedded = $template_download_embedded_temp;
			if(($file->file_permission == 1 && intval($user_ID) > 0) || $file->file_permission == 0) {
				$template_download_embedded = stripslashes($template_download_embedded[0]);
			} else {
				$template_download_embedded = stripslashes($template_download_embedded[1]);
			}
			$template_download_embedded = str_replace("%FILE_ID%", $file->file_id, $template_download_embedded);
			$template_download_embedded = str_replace("%FILE%", stripslashes($file->file), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_NAME%", stripslashes($file->file_name), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_ICON%", file_extension_image(stripslashes($file->file), $file_extensions_images), $template_download_embedded);
			if($display == 'both') {
				$template_download_embedded = str_replace("%FILE_DESCRIPTION%",  stripslashes($file->file_des), $template_download_embedded);
			} else {
				$template_download_embedded = str_replace("%FILE_DESCRIPTION%",  '', $template_download_embedded);
			}
			$template_download_embedded = str_replace("%FILE_SIZE%",  format_filesize($file->file_size), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[intval($file->file_category)]), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_UPDATED_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_UPDATED_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_HITS%", number_format_i18n($file->file_hits), $template_download_embedded);
			$template_download_embedded = str_replace("%FILE_DOWNLOAD_URL%", download_file_url($file->file_id), $template_download_embedded);	
			$output .= $template_download_embedded; 
		}
		return apply_filters('download_embedded', $output);
	}
}


### Function: Get Most Downloaded Files
if(!function_exists('get_most_downloaded')) {
	function get_most_downloaded($limit = 10, $chars = 0, $display = true) {
		global $wpdb, $user_ID;
		$output = '';
		$file_extensions_images = file_extension_images();
		$files = $wpdb->get_results("SELECT * FROM $wpdb->downloads WHERE file_permission != -1 ORDER BY file_hits DESC LIMIT $limit");
		if($files) {
			foreach($files as $file) {
				// Cet Download Categories
				$download_categories = get_option('download_categories');
				// Get Most Downloaded
				$template_download_most = get_option('download_template_most');
				if(($file->file_permission == 1 && intval($user_ID) > 0) || $file->file_permission == 0) {
					$template_download_most = stripslashes($template_download_most[0]);
				} else {
					$template_download_most = stripslashes($template_download_most[1]);
				}
				if($chars > 0) {
					$file_name = snippet_text(stripslashes($file->file_name), $chars);
				} else {
					$file_name = stripslashes($file->file_name);
				}
				$template_download_most = str_replace("%FILE_ID%", $file->file_id, $template_download_most);
				$template_download_most = str_replace("%FILE%", stripslashes($file->file), $template_download_most);
				$template_download_most = str_replace("%FILE_NAME%", $file_name, $template_download_most);
				$template_download_most = str_replace("%FILE_ICON%", file_extension_image(stripslashes($file->file), $file_extensions_images), $template_download_most);
				$template_download_most = str_replace("%FILE_DESCRIPTION%",  stripslashes($file->file_des), $template_download_most);
				$template_download_most = str_replace("%FILE_SIZE%",  format_filesize($file->file_size), $template_download_most);
				$template_download_most = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[intval($file->file_category)]), $template_download_most);
				$template_download_most = str_replace("%FILE_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_UPDATED_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_UPDATED_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_HITS%", number_format_i18n($file->file_hits), $template_download_most);
				$template_download_most = str_replace("%FILE_DOWNLOAD_URL%", download_file_url($file->file_id), $template_download_most);
				$output .= $template_download_most;
			}
		} else {
			$output = '<li>'.__('N/A', 'wp-downloadmanager').'</li>'."\n";
		}
		if($display) {
			echo $output;
		} else {
			return $output;
		}
	}
}


### Function: Get Newest Downloads
if(!function_exists('get_recent_downloads')) {
	function get_recent_downloads($limit = 10, $chars = 0, $display = true) {
		global $wpdb, $user_ID;
		$output = '';
		$file_extensions_images = file_extension_images();
		$files = $wpdb->get_results("SELECT * FROM $wpdb->downloads WHERE file_permission != -1 ORDER BY file_date DESC LIMIT $limit");
		if($files) {
			foreach($files as $file) {
				// Cet Download Categories
				$download_categories = get_option('download_categories');
				// Get Newest Downloads
				$template_download_most = get_option('download_template_most');
				if(($file->file_permission == 1 && intval($user_ID) > 0) || $file->file_permission == 0) {
					$template_download_most = stripslashes($template_download_most[0]);
				} else {
					$template_download_most = stripslashes($template_download_most[1]);
				}
				if($chars > 0) {
					$file_name = snippet_text(stripslashes($file->file_name), $chars);
				} else {
					$file_name = stripslashes($file->file_name);
				}
				$template_download_most = str_replace("%FILE_ID%", $file->file_id, $template_download_most);
				$template_download_most = str_replace("%FILE%", stripslashes($file->file), $template_download_most);
				$template_download_most = str_replace("%FILE_NAME%", $file_name, $template_download_most);
				$template_download_most = str_replace("%FILE_ICON%", file_extension_image(stripslashes($file->file), $file_extensions_images), $template_download_most);
				$template_download_most = str_replace("%FILE_DESCRIPTION%",  stripslashes($file->file_des), $template_download_most);
				$template_download_most = str_replace("%FILE_SIZE%",  format_filesize($file->file_size), $template_download_most);
				$template_download_most = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[intval($file->file_category)]), $template_download_most);
				$template_download_most = str_replace("%FILE_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_UPDATED_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_UPDATED_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_HITS%", number_format_i18n($file->file_hits), $template_download_most);
				$template_download_most = str_replace("%FILE_DOWNLOAD_URL%", download_file_url($file->file_id), $template_download_most);
				$output .= $template_download_most;
			}
		} else {
			$output = '<li>'.__('N/A', 'wp-downloadmanager').'</li>'."\n";
		}
		if($display) {
			echo $output;
		} else {
			return $output;
		}
	}
}


### Function: Get Downloads By Category ID
if(!function_exists('get_downloads_category')) {
	function get_downloads_category($cat_id = 1, $limit = 10, $chars = 0, $display = true) {
		global $wpdb, $user_ID;
		$cat_id = intval($cat_id);
		$output = '';
		$file_extensions_images = file_extension_images();
		$files = $wpdb->get_results("SELECT * FROM $wpdb->downloads WHERE file_category = $cat_id AND file_permission != -1 ORDER BY file_date DESC LIMIT $limit");
		if($files) {
			foreach($files as $file) {
				// Cet Download Categories
				$download_categories = get_option('download_categories');
				// Get Downloads By Category ID
				$template_download_most = get_option('download_template_most');
				if(($file->file_permission == 1 && intval($user_ID) > 0) || $file->file_permission == 0) {
					$template_download_most = stripslashes($template_download_most[0]);
				} else {
					$template_download_most = stripslashes($template_download_most[1]);
				}
				if($chars > 0) {
					$file_name = snippet_text(stripslashes($file->file_name), $chars);
				} else {
					$file_name = stripslashes($file->file_name);
				}
				$template_download_most = str_replace("%FILE_ID%", $file->file_id, $template_download_most);
				$template_download_most = str_replace("%FILE%", stripslashes($file->file), $template_download_most);
				$template_download_most = str_replace("%FILE_NAME%", $file_name, $template_download_most);
				$template_download_most = str_replace("%FILE_ICON%", file_extension_image(stripslashes($file->file), $file_extensions_images), $template_download_most);
				$template_download_most = str_replace("%FILE_DESCRIPTION%",  stripslashes($file->file_des), $template_download_most);
				$template_download_most = str_replace("%FILE_SIZE%",  format_filesize($file->file_size), $template_download_most);
				$template_download_most = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[intval($file->file_category)]), $template_download_most);
				$template_download_most = str_replace("%FILE_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_UPDATED_DATE%",  mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_UPDATED_TIME%",  mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_most);
				$template_download_most = str_replace("%FILE_HITS%", number_format_i18n($file->file_hits), $template_download_most);
				$template_download_most = str_replace("%FILE_DOWNLOAD_URL%", download_file_url($file->file_id), $template_download_most);
				$output .= $template_download_most;
			}
		} else {
			$output = '<li>'.__('N/A', 'wp-downloadmanager').'</li>'."\n";
		}
		if($display) {
			echo $output;
		} else {
			return $output;
		}
	}
}


### Function: Plug Into WP-Stats
if(strpos(get_option('stats_url'), $_SERVER['REQUEST_URI']) || strpos($_SERVER['REQUEST_URI'], 'stats-options.php') || strpos($_SERVER['REQUEST_URI'], 'wp-stats/wp-stats.php')) {
	add_filter('wp_stats_page_admin_plugins', 'downloadmanager_page_admin_general_stats');
	add_filter('wp_stats_page_admin_recent', 'downloadmanager_page_admin_recent_stats');
	add_filter('wp_stats_page_admin_most', 'downloadmanager_page_admin_most_stats');
	add_filter('wp_stats_page_plugins', 'downloadmanager_page_general_stats');
	add_filter('wp_stats_page_recent', 'downloadmanager_page_recent_stats');
	add_filter('wp_stats_page_most', 'downloadmanager_page_most_stats');
}


### Function: Add WP-DownloadManager General Stats To WP-Stats Page Options
function downloadmanager_page_admin_general_stats($content) {
	$stats_display = get_option('stats_display');
	if($stats_display['downloads'] == 1) {
		$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_downloads" value="downloads" checked="checked" />&nbsp;&nbsp;<label for="wpstats_downloads">'.__('WP-DownloadManager', 'wp-downloadmanager').'</label><br />'."\n";
	} else {
		$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_downloads" value="downloads" />&nbsp;&nbsp;<label for="wpstats_downloads">'.__('WP-DownloadManager', 'wp-downloadmanager').'</label><br />'."\n";
	}
	return $content;
}


### Function: Add WP-DownloadManager Top Recent Stats To WP-Stats Page Options
function downloadmanager_page_admin_recent_stats($content) {
	$stats_display = get_option('stats_display');
	$stats_mostlimit = intval(get_option('stats_mostlimit'));
	if($stats_display['recent_downloads'] == 1) {
		$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_recent_downloads" value="recent_downloads" checked="checked" />&nbsp;&nbsp;<label for="wpstats_recent_downloads">'.sprintf(__ngettext('%s Most Recent Download', '%s Most Recent Downloads', $stats_mostlimit, 'wp-downloadmanager'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
	} else {
		$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_recent_downloads" value="recent_downloads" />&nbsp;&nbsp;<label for="wpstats_recent_downloads">'.sprintf(__ngettext('%s Most Recent Download', '%s Most Recent Downloads', $stats_mostlimit, 'wp-downloadmanager'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
	}
	return $content;
}


### Function: Add WP-DownloadManager Top Most/Highest Stats To WP-Stats Page Options
function downloadmanager_page_admin_most_stats($content) {
	$stats_display = get_option('stats_display');
	$stats_mostlimit = intval(get_option('stats_mostlimit'));
	if($stats_display['downloaded_most'] == 1) {
		$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_downloaded_most" value="downloaded_most" checked="checked" />&nbsp;&nbsp;<label for="wpstats_downloaded_most">'.sprintf(__ngettext('%s Most Downloaded File', '%s Most Downloaded Files', $stats_mostlimit, 'wp-downloadmanager'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
	} else {
		$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_downloaded_most" value="downloaded_most" />&nbsp;&nbsp;<label for="wpstats_downloaded_most">'.sprintf(__ngettext('%s Most Downloaded File', '%s Most Downloaded Files', $stats_mostlimit, 'wp-downloadmanager'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
	}
	return $content;
}


### Function: Add WP-DownloadManager General Stats To WP-Stats Page
function downloadmanager_page_general_stats($content) {
	global $wpdb;
	$stats_display = get_option('stats_display');
	if($stats_display['downloads'] == 1) {
		$download_stats = $wpdb->get_row("SELECT COUNT(file_id) as total_files, SUM(file_size) total_size, SUM(file_hits) as total_hits FROM $wpdb->downloads");
		$content .= '<p><strong>'.__('WP-DownloadManager', 'wp-downloadmanager').'</strong></p>'."\n";
		$content .= '<ul>'."\n";
		$content .= '<li>'.sprintf(__ngettext('<strong>%s</strong> file was added.', '<strong>%s</strong> files were added.', $download_stats->total_files, 'wp-downloadmanager'), number_format_i18n($download_stats->total_files)).'</li>'."\n";
		$content .= '<li>'.sprintf(__ngettext('<strong>%s</strong> worth of files.', '<strong>%s</strong> worth of files.', $download_stats->total_size, 'wp-downloadmanager'), format_filesize($download_stats->total_size)).'</li>'."\n";
		$content .= '<li>'.sprintf(__ngettext('<strong>%s</strong> hit was generated.', '<strong>%s</strong> hits were generated.', $download_stats->total_hits, 'wp-downloadmanager'), number_format_i18n($download_stats->total_hits)).'</li>'."\n";
		$content .= '</ul>'."\n";
	}
	return $content;
}


### Function: Add WP-DownloadManager Top Recent Stats To WP-Stats Page
function downloadmanager_page_recent_stats($content) {
	$stats_display = get_option('stats_display');
	$stats_mostlimit = intval(get_option('stats_mostlimit'));
	if($stats_display['recent_downloads'] == 1) {
		$content .= '<p><strong>'.sprintf(__ngettext('%s Most Recent Download', '%s Most Recent Downloads', $stats_mostlimit, 'wp-downloadmanager'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
		$content .= '<ul>'."\n";
		$content .= get_recent_downloads($stats_mostlimit, 0, false);
		$content .= '</ul>'."\n";
	}
	return $content;
}


### Function: Add WP-DownloadManager Top Most/Highest Stats To WP-Stats Page
function downloadmanager_page_most_stats($content) {
	$stats_display = get_option('stats_display');
	$stats_mostlimit = intval(get_option('stats_mostlimit'));
	if($stats_display['downloaded_most'] == 1) {
		$content .= '<p><strong>'.sprintf(__ngettext('%s Most Downloaded File', '%s Most Downloaded Files', $stats_mostlimit, 'wp-downloadmanager'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
		$content .= '<ul>'."\n";
		$content .= get_most_downloaded($stats_mostlimit, 0, false);
		$content .= '</ul>'."\n";
	}
	return $content;
}


### Function: Create Downloads Table
add_action('activate_wp-downloadmanager/wp-downloadmanager.php', 'create_download_table');
function create_download_table() {
	global $wpdb, $blog_id;
	downloadmanager_textdomain();
	if(@is_file(ABSPATH.'/wp-admin/upgrade-functions.php')) {
		include_once(ABSPATH.'/wp-admin/upgrade-functions.php');
	} elseif(@is_file(ABSPATH.'/wp-admin/includes/upgrade.php')) {
		include_once(ABSPATH.'/wp-admin/includes/upgrade.php');
	} else {
		die('We have problem finding your \'/wp-admin/upgrade-functions.php\' and \'/wp-admin/includes/upgrade.php\'');
	}
	$charset_collate = '';
	if($wpdb->supports_collation()) {
		if(!empty($wpdb->charset)) {
			$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
		}
		if(!empty($wpdb->collate)) {
			$charset_collate .= " COLLATE $wpdb->collate";
		}
	}
	// Create WP-Downloads Table
	$create_table = "CREATE TABLE $wpdb->downloads (".
							"file_id int(10) NOT NULL auto_increment,".
							"file tinytext NOT NULL,".
							"file_name text character set utf8 NOT NULL,".
							"file_des text character set utf8 NOT NULL,".
							"file_size varchar(20) NOT NULL default '',".
							"file_category int(2) NOT NULL default '0',".
							"file_date varchar(20) NOT NULL default '',".
							"file_updated_date varchar(20) NOT NULL default '',".
							"file_last_downloaded_date varchar(20) NOT NULL default '',".
							"file_hits int(10) NOT NULL default '0',".
							"file_permission TINYINT(2) NOT NULL default '0',".
							"PRIMARY KEY (file_id)) $charset_collate;";
	maybe_create_table($wpdb->downloads, $create_table);
	// WP-Downloads Options
	if (function_exists('is_site_admin')) {
		add_option('download_path', WP_CONTENT_DIR.'/blogs.dir/'.$blog_id.'/files', 'Download Path');
		add_option('download_path_url', WP_CONTENT_URL.'/blogs.dir/'. $blog_id.'/files', 'Download Path URL');
	} else {
		add_option('download_path', WP_CONTENT_DIR.'/files', 'Download Path');
		add_option('download_path_url', content_url('files'), 'Download Path URL');
	}
	add_option('download_page_url', site_url('downloads'), 'Download Page URL');
	add_option('download_method', 0, 'Download Type');
	add_option('download_categories', array('General'), 'Download Categories');
	add_option('download_sort', array('by' => 'file_name', 'order' => 'asc', 'perpage' => 20, 'group' => 1), 'Download Sorting Options');
	add_option('download_template_header', '<p>'.__('There are <strong>%TOTAL_FILES_COUNT% files</strong>, weighing <strong>%TOTAL_SIZE%</strong> with <strong>%TOTAL_HITS% hits</strong> in <strong>%FILE_CATEGORY_NAME%</strong>.</p><p>Displaying <strong>%RECORD_START%</strong> to <strong>%RECORD_END%</strong> of <strong>%TOTAL_FILES_COUNT%</strong> files.', 'wp-downloadmanager').'</p>', 'Download Page Header Template');
	add_option('download_template_footer', '<form action="%DOWNLOAD_PAGE_URL%" method="get"><p><input type="hidden" name="dl_cat" value="%CATEGORY_ID%" /><input type="text" name="dl_search" value="%FILE_SEARCH_WORD%" />&nbsp;&nbsp;&nbsp;<input type="submit" value="'.__('Search', 'wp-downloadmanager').'" /></p></form>', 'Download Page Footer Template');
	add_option('download_template_category_header', '<h2 id="downloadcat-%CATEGORY_ID%"><a href="%CATEGORY_URL%" title="'.__('View all downloads in %FILE_CATEGORY_NAME%', 'wp-downloadmanager').'">%FILE_CATEGORY_NAME%</a></h2>', 'Download Category Header Template');
	add_option('download_template_category_footer', '', 'Download Category Footer Template');
	add_option('download_template_listing', array('<p><img src="'.plugins_url('wp-downloadmanager/images').'/%FILE_ICON%" alt="" title="" style="vertical-align: middle;" />&nbsp;&nbsp;<strong><a href="%FILE_DOWNLOAD_URL%">%FILE_NAME%</a></strong><br /><strong>&raquo; %FILE_SIZE% - %FILE_HITS% '.__('hits', 'wp-downloadmanager').' - %FILE_DATE%</strong><br />%FILE_DESCRIPTION%</p>', '<p><img src="'.plugins_url('wp-downloadmanager/images').'/%FILE_ICON%" alt="" title="" style="vertical-align: middle;" />&nbsp;&nbsp;<strong>%FILE_NAME%</strong><br /><strong>&raquo; %FILE_SIZE% - %FILE_HITS% '.__('hits', 'wp-downloadmanager').' - %FILE_DATE%</strong><br /><i>'.__('You need to be a registered user to download this file.', 'wp-downloadmanager').'</i><br />%FILE_DESCRIPTION%</p>'), 'Download Listing Template');
	add_option('download_template_embedded', array('<p><img src="'.plugins_url('wp-downloadmanager/images').'/%FILE_ICON%" alt="" title="" style="vertical-align: middle;" />&nbsp;&nbsp;<strong><a href="%FILE_DOWNLOAD_URL%">%FILE_NAME%</a></strong> (%FILE_SIZE%'.__(',', 'wp-downloadmanager').' %FILE_HITS% '.__('hits', 'wp-downloadmanager').')</p>', '<p><img src="'.plugins_url('wp-downloadmanager/images').'/%FILE_ICON%" alt="" title="" style="vertical-align: middle;" />&nbsp;&nbsp;<strong>%FILE_NAME%</strong> (%FILE_SIZE%'.__(',', 'wp-downloadmanager').' %FILE_HITS% '.__('hits', 'wp-downloadmanager').')<br /><i>'.__('You need to be a registered user to download this file.', 'wp-downloadmanager').'</i></p>'), 'Download Embedded Template');
	add_option('download_template_most', array('<li><a href="%FILE_DOWNLOAD_URL%">%FILE_NAME%</a> (%FILE_SIZE%'.__(',', 'wp-downloadmanager').' %FILE_HITS% '.__('hits', 'wp-downloadmanager').')</li>', '<li>%FILE_NAME% (%FILE_SIZE%'.__(',', 'wp-downloadmanager').' %FILE_HITS% '.__('hits', 'wp-downloadmanager').')<br /><i>'.__('You need to be a registered user to download this file.', 'wp-downloadmanager').'</i></li>'), 'Most Download Template');
	// Database Upgrade For WP-Download 1.30
	$check_for_130 = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'download_nice_permalink'");
	if(!$check_for_130) {
		maybe_add_column($wpdb->downloads, 'file_updated_date', "ALTER TABLE $wpdb->downloads ADD file_updated_date VARCHAR(20) NOT NULL AFTER file_date;");
		$wpdb->query("UPDATE $wpdb->downloads SET file_updated_date = file_date;");
		maybe_add_column($wpdb->downloads, 'file_last_downloaded_date', "ALTER TABLE $wpdb->downloads ADD file_last_downloaded_date VARCHAR(20) NOT NULL AFTER file_updated_date;");
		$wpdb->query("UPDATE $wpdb->downloads SET file_last_downloaded_date = file_date;");
	}
	add_option('download_template_pagingheader', '', 'Displayed Before Paging In The Downloads Page');
	add_option('download_template_pagingfooter', '', 'Displayed After Paging In The Downloads Page');
	add_option('download_nice_permalink', 1, 'Use Download Nice Permalink');
	add_option('download_template_download_page_link', '<p><a href="%DOWNLOAD_PAGE_URL%" title="'.__('Downloads Page', 'wp-downloadmanager').'">'.__('Downloads Page', 'wp-downloadmanager').'</a></p>', 'Template For Download Page Link');
	add_option('download_template_none', '<p style="text-align: center;">'.__('No Files Found.', 'wp-downloadmanager').'</p>', 'Template For No Downloads Found');
	// Create Files Folder
	if (function_exists('is_site_admin')) {
		if(!is_dir(WP_CONTENT_DIR.'/blogs.dir/'.$blog_id.'/files/')) {
			mkdir(WP_CONTENT_DIR.'/blogs.dir/'.$blog_id.'/files/', 0777, true);
		}
	} else {
		if(!is_dir(WP_CONTENT_DIR.'/files/')) {
			mkdir(WP_CONTENT_DIR.'/files/', 0777, true);
		}
	}
	// Set 'manage_downloads' Capabilities To Administrator	
	$role = get_role('administrator');
	if(!$role->has_cap('manage_downloads')) {
		$role->add_cap('manage_downloads');
	}
}
?>